package gui;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JPanel;

import main.FileManager;
import main.MusicFile;
import mp3.MP3ConversionManager;
import ftp.FTPController;



public class CalendarPanel extends JPanel {

	/**
	 * Create the panel.
	 */
	public CalendarPanel() {

	}

	ArrayList<MusicFile> filesToDisplay;
	int selectedYear = 2012;
	int selectedMonth = 6;


	public static void main(String args[]){
		JFrame frame = new JFrame("Oval Sample");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.setLayout(new GridLayout(2, 2));

		CalendarPanel t = new CalendarPanel();
		frame.add(t);
		t.setVisible(true);

		frame.setSize(300, 200);
		frame.setVisible(true);
	}
	@Override
	public void paintComponent (Graphics g) {

		super.paintComponents(g);
 
		 filesToDisplay = FileManager.getListForMonth(selectedYear, selectedMonth);


		Calendar t = Calendar.getInstance();
		t.set(selectedYear, selectedMonth -1, 1);

		int endDay = t.getActualMaximum(Calendar.DAY_OF_MONTH);
		int startDay = t.get(Calendar.DAY_OF_WEEK) - 1 > 0 ? t.get(Calendar.DAY_OF_WEEK) - 1 : 7;




		int dayCounter = 1;
		int c = 0;


		
		 for(MusicFile f :filesToDisplay) {
			int  temp  = f.airDate.get(Calendar.DAY_OF_MONTH);
			int x =  (temp -1 + startDay-1) %7;
			int y = (temp-1)/7 + ((temp%7 ==0 ? 7 :temp%7 + startDay-1) > 7 ? 1:0);	

			int per;
		 switch(f.transition)
		 {
		 case NONE:
			 		g.setColor(Color.GREEN);
		 g.fillRect(x*this.getSize().width/7  +1 , y*this.getSize().height/6 +1,this.getSize().width/7-1 ,this.getSize().height/6-1 );
		 break;	
		 case WAVE_MP3:
			 g.setColor(Color.RED);
			  per = MP3ConversionManager.mp3ConversionManager.getPercentageComplete(f);
			g.fillRect(x*this.getSize().width/7  +1, (int)(y*this.getSize().height/6 +1 + (this.getSize().height/6-1)* (1-per/100.0)), this.getSize().width/7-1, (int)((this.getSize().height/6-1)* (per/100.0)));
			 break;
			 case FTP : 
				 g.setColor(Color.YELLOW);
				  per = FTPController.getInstance().getPercentageComplete(f);
					g.fillRect(x*this.getSize().width/7  +1, (int)(y*this.getSize().height/6 +1 + (this.getSize().height/6-1)* (1-per/100.0)), this.getSize().width/7-1, (int)((this.getSize().height/6-1)* (per/100.0)));
		 }

		 g.setColor(Color.black);
		 g.drawString(f.name,x*this.getSize().width/7 +10 , y*this.getSize().height/6 +20 );
		 
	
 
		 }
		 
		 
		for(int j = 0; j < 6 ; j++) {
			for(int i = 0; i < 7 ; i++) 
			{
				g.drawRect(i*this.getSize().width/7 , j*this.getSize().height/6 , this.getSize().width/7, this.getSize().height/6);

				if (dayCounter >= startDay && c<  endDay) {c++;
				g.drawString(""+ c,i*this.getSize().width/7 + 5, j*this.getSize().height/6 + 10 );
				}
			//	System.out.println(c +" " + dayCounter + " " + endDay + " " +(dayCounter<= endDay));
				dayCounter++;	

			}

		}
	}

int temp;
	void incrementMonth() {
		incrementMonth(1);
	}

	void incrementMonth(int month){
		selectedMonth = selectedMonth + month  % 12;
		selectedMonth = selectedMonth == 0 ? 12 : selectedMonth;
		this.updateUI();
	}
	public Calendar drop(int mouseX, int mouseY) {
		 
		int relX = mouseX - getX();
		int relY = mouseY - getY();
		int horizontalBox =  (int) (relX /(getWidth()/7.0));
		int verticalBox =  (int) (relY /(getHeight()/6.0));
		int boxes = verticalBox * 7 + horizontalBox  + 2;
		
		Calendar t = Calendar.getInstance();
		t.set(selectedYear, selectedMonth -1, 1);

		int endDay = t.getActualMaximum(Calendar.DAY_OF_MONTH);
		int startDay = t.get(Calendar.DAY_OF_WEEK) - 1 > 0 ? t.get(Calendar.DAY_OF_WEEK) - 1 : 7;
		t.set(Calendar.MONTH, selectedMonth);
		t.set(Calendar.DAY_OF_MONTH , boxes - startDay);
		return t;
	}
	public void decrementMonth() {
	selectedMonth--;
	repaint();
	}

}
